package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.BuyerAbnormolListActivity;
import com.sxm.yiwei.activitis.BuyerInServiceListActivity;
import com.sxm.yiwei.activitis.BuyerNoDiscusslistActivity;
import com.sxm.yiwei.activitis.BuyerNoOverBookActivity;
import com.sxm.yiwei.activitis.BuyerNoPayListActivity;
import com.sxm.yiwei.activitis.BuyerOverListActivity;
import com.sxm.yiwei.activitis.BuyerServiceListActivity;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/** 买家订单
 * A simple {@link Fragment} subclass.
 */
public class ShopBuyerFragment extends Fragment implements View.OnClickListener{


    private RelativeLayout nopay;
    private RelativeLayout noservice;
    private RelativeLayout inservice;
    private RelativeLayout noover;
    private RelativeLayout over;
    private RelativeLayout nodiscuss;
    private RelativeLayout abnormal;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private TextView service1,service2,service3,service4,service5,service6;

    public ShopBuyerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onStart() {
        super.onStart();
        getBuyerBookList();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop_buyer, container, false);

        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        initBuyerView(view);
        getBuyerBookList();
        return view;
    }
        // 获取买家订单信息
    private void getBuyerBookList() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETBUYERBOOKNUMBER);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                JSONObject obj1 = JSON.parseObject(data);
                String pendingPayment = obj1.getString("pendingPayment");
                String complaintCount = obj1.getString("complaintCount");
                String inService = obj1.getString("inService");
                String complete = obj1.getString("complete");
                String end = obj1.getString("end");
                String pendingService = obj1.getString("pendingService");

                if (pendingPayment==null){
                    service2.setText("0");
                }else {
                    service2.setText(pendingPayment);
                }
                if (pendingService==null){
                    service3.setText("0");
                }else {
                    service3.setText(pendingService);
                }
                if (inService==null){
                    service4.setText("0");
                }else {
                    service4.setText(inService);
                }
                if (complete==null){
                    service5.setText("0");
                }else {
                    service5.setText(complete);
                }
                if (complaintCount==null){
                    service1.setText("0");
                }else {
                    service1.setText(complaintCount);
                }
                if (end==null){
                    service6.setText("0");
                }else {
                    service6.setText(end);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(getActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initBuyerView(View view) {

        nopay = (RelativeLayout) view.findViewById(R.id.buyer_nopaybook);
        noservice = (RelativeLayout) view.findViewById(R.id.buyer_noservicebook);
        inservice = (RelativeLayout) view.findViewById(R.id.buyer_inservicebook);
        noover = (RelativeLayout) view.findViewById(R.id.buyer_nooverbook);
        over = (RelativeLayout) view.findViewById(R.id.buyer_overbook);
        abnormal = (RelativeLayout) view.findViewById(R.id.buyer_abnormalbook);

        service1 = (TextView) view.findViewById(R.id.buyer_service1);
        service2 =(TextView) view.findViewById(R.id.buyer_service2);
        service3 =(TextView) view.findViewById(R.id.buyer_service3);
        service4 =(TextView) view.findViewById(R.id.buyer_service4);
        service5 =(TextView) view.findViewById(R.id.buyer_service5);
        service6 =(TextView) view.findViewById(R.id.buyer_service6);

        nopay.setOnClickListener(this);
        noservice.setOnClickListener(this);
        inservice.setOnClickListener(this);
        noover.setOnClickListener(this);
        over.setOnClickListener(this);
        abnormal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buyer_nopaybook:
                startActivity(new Intent(getActivity(), BuyerNoPayListActivity.class));
                break;
            case R.id.buyer_noservicebook:
                // 待服务
                startActivity(new Intent(getActivity(), BuyerServiceListActivity.class));
                break;
            case R.id.buyer_inservicebook:
                startActivity(new Intent(getActivity(), BuyerInServiceListActivity.class));
                break;
            case R.id.buyer_nooverbook:
                startActivity(new Intent(getActivity(), BuyerNoOverBookActivity.class));
                break;
            case R.id.buyer_overbook:
                startActivity(new Intent(getActivity(), BuyerOverListActivity.class));
                break;
            case R.id.buyer_abnormalbook:
                startActivity(new Intent(getActivity(), BuyerAbnormolListActivity.class));
                break;
        }
    }
}
